//
//  SecondViewController.m
//  A段 准备
//
//  Created by 凯 on 15/7/11.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "SecondViewController.h"
#import "BaseHeader.h"
#import "UIImageView+WebCache.h"
#import "AFNetWorkHandle.h"
#import "UMSocial.h"

@interface SecondViewController ()<UIWebViewDelegate,UIScrollViewDelegate>

//详情攻略页面

@property (nonatomic , retain)UIWebView *webview;


@property (nonatomic , retain)UIView *MyView;

@property (nonatomic , retain)UIImageView *imageview;


@property (nonatomic , retain)UILabel *label;

@property (nonatomic , retain)UIView *Navview;

@property (nonatomic , retain)UILabel *Navlabel;

@property (nonatomic , retain)UIButton *back;

@property (nonatomic , retain)NSString *push;


@property (nonatomic , retain)UIButton *pushbutton;


@property (nonatomic , retain)UIButton *collectbutton;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.translucent = NO;
    self.webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, -36, SCREEN_WIDTH, SCREEN_HEIGHT + 200)];
    [self getdata];
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:self.webstring]];
    self.webview.scrollView.bounces = NO;
    
    self.webview.delegate = self;
    [self.webview loadRequest:request];
    [self.view addSubview:self.webview];

    [self creatTabbar];
    [self creatNav];
    [self getbutton];


    [self.webview release];
}
-(void)getdata
{
//    if ([self.y isEqualToString:@"1"]) {
    
    
    NSString *str = [NSString stringWithFormat:@"%@",self.web];
    NSString *pushstr = [@"http://api.liwushuo.com/v2/items/" stringByAppendingString:str];
    self.webstring =  [@"http://www.liwushuo.com/items/" stringByAppendingString:str];
    [AFNetWorkHandle GETwithAFNByURL:pushstr completion:^(id result) {
        self.push = [[result objectForKey:@"data"]objectForKey:@"purchase_url"];
        NSLog(@"%@",self.push);
    }];
        
//    } if ([self.y isEqualToString:@"2"]) {
//        self.webstring = self.attackwebstring;
//    }
}

//-(void)creatview
//{
//    self.x = 0;
////    self.MyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2.1)];
////    NSLog(@"%f", self.MyView.frame.size.height);
////    //        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
////    self.MyView.backgroundColor = [UIColor blueColor];
////    [self.webview.scrollView addSubview:self.MyView];
//    self.webview.scrollView.delegate = self;
//#pragma mark - 放大imageview  还没写好呢
////    self.webview.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
//    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.4)];
//    
//    self.label = [[UILabel alloc]initWithFrame:CGRectMake( 0, self.imageview.frame.size.height - 100, SCREEN_WIDTH, 100)];
//    self.label.text = self.labeltext;
//    self.label.textColor = [UIColor whiteColor];
//    self.label.font = [UIFont systemFontOfSize:22];
//    self.label.numberOfLines = 0;
//  //  self.label.textAlignment = UITextAlignmentCenter;
//    self.label.backgroundColor = [UIColor clearColor];
//    self.imageview.backgroundColor = [UIColor clearColor];
//    [self.imageview setImageWithURL:[NSURL URLWithString:self.imageName]] ;
//    self.imageview.userInteractionEnabled = YES;
//   // self.imageview.contentMode =UIViewContentModeScaleAspectFill;
//    self.pushbutton = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.pushbutton.frame = CGRectMake(SCREEN_WIDTH - 100, self.imageview
//                                       .frame.size.height - 80, 100, 50);
//    self.pushbutton.backgroundColor = [UIColor blueColor];
//    [self.pushbutton addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
//    
//    [self.view addSubview:self.pushbutton];
//    [self.webview.scrollView addSubview:self.imageview];
//    [self.webview.scrollView bringSubviewToFront:self.pushbutton];
//    [self.imageview addSubview:self.label];
//    [self creatNav];
//}

-(void)creatview
{
    self.x = 0;
//    self.MyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2.1)];
//    NSLog(@"%f", self.MyView.frame.size.height);
//    //        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
//    self.MyView.backgroundColor = [UIColor blueColor];
//    [self.webview.scrollView addSubview:self.MyView];
    self.webview.scrollView.delegate = self;
#pragma mark - 放大imageview  还没写好呢
//    self.webview.scrollView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
    self.imageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.4)];
    
    self.label = [[UILabel alloc]initWithFrame:CGRectMake( 0, self.imageview.frame.size.height - 100, SCREEN_WIDTH, 100)];
    self.label.text = self.labeltext;
    self.label.textColor = [UIColor whiteColor];
    self.label.font = [UIFont systemFontOfSize:22];
    self.label.numberOfLines = 0;
    self.label.textAlignment = UITextAlignmentCenter;
    self.label.backgroundColor = [UIColor clearColor];
    self.imageview.backgroundColor = [UIColor clearColor];
    [self.imageview setImageWithURL:[NSURL URLWithString:self.imageName]] ;
    self.imageview.userInteractionEnabled = YES;
   // self.imageview.contentMode =UIViewContentModeScaleAspectFill;
    self.pushbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.pushbutton.frame = CGRectMake(SCREEN_WIDTH - 100, self.imageview
                                       .frame.size.height - 80, 100, 50);
    self.pushbutton.backgroundColor = [UIColor blueColor];
    [self.pushbutton addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.pushbutton];
    [self.webview.scrollView addSubview:self.imageview];
    [self.webview.scrollView bringSubviewToFront:self.pushbutton];
    [self.imageview addSubview:self.label];
    [self creatNav];
}

- (void) webViewDidStartLoad:(UIWebView *)webView
{
//    NSLog(@"webViewDidStartLoad");
//    if (self.x == YES) {
//        [self.imageview removeFromSuperview];
//    }
//    NSString *str = self.webview.request.URL.absoluteString;
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:str]];
  //   webView.request.URL.absoluteString;
   // [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
//    self.x = YES;
//    NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError:%@", error);
}



-(void)creatNav
{
    self.Navview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20 )];
    self.Navview.backgroundColor = NAVCOLOR;
    [self.view addSubview:self.Navview];
    self.Navlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, SCREEN_WIDTH, 44)];
//    self.Navlabel.alpha = 0;
    self.Navlabel.backgroundColor = NAVCOLOR;
    self.Navlabel.text = self.labeltext;
    self.Navlabel.numberOfLines = 0;
    self.Navlabel.font = [UIFont systemFontOfSize:17];
    self.Navlabel.textColor = [UIColor whiteColor];
    self.Navlabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.Navlabel];
    [self getbutton];
}
-(void)getbutton
{
    self.back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.back.backgroundColor = [UIColor clearColor];
    self.back.frame = CGRectMake(5, 32, 20, 20);
    [self.back setBackgroundImage:[UIImage imageNamed:@"iconfont-xiangzuo@2x"] forState:UIControlStateNormal];
    [self.back addTarget:self action:@selector(backaaa) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.back];
}

//
//-(void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//    if (self.x == 0) {
//    
////    NSLog(@"%f", scrollView.contentOffset.x);
//    
//    if (scrollView.contentOffset.y > SCREEN_HEIGHT / 1.4 - 64 - 100) {
//        //imageview 在偏移出屏幕的时候 imageview的底边显示为 navlabel的背景
//        self.imageview.frame = CGRectMake(0, - SCREEN_HEIGHT / 1.4 + 64, SCREEN_WIDTH, SCREEN_HEIGHT / 1.4);
//        [self.view addSubview:self.imageview];
//        //假的导航栏一直在最上层视图
//        [self.view bringSubviewToFront:self.Navlabel];
////         [self getbutton];
//    } else if (scrollView.contentOffset.y < - 20) {
//        CGRect frame = self.imageview.frame;
//        CGFloat f =   frame.size.width / frame.size.height;
//        frame.origin.y = 20 + scrollView.contentOffset.y;
//        
//        frame.size.height = SCREEN_HEIGHT / 1.4 - scrollView.contentOffset.y - 20;
//        frame.size.width = f * frame.size.height;
//        frame.origin.x = - (frame.size.width - SCREEN_WIDTH) / 2;
//        self.imageview.frame = frame;
//        
//        
//        self.label.frame = CGRectMake((frame.size.width - SCREEN_WIDTH) / 2, SCREEN_HEIGHT / 1.5 - 130 - scrollView.contentOffset.y, self.label.frame.size.width, self.label.frame.size.height);
//    } else {
//        CGFloat f = (scrollView.contentOffset.y + 20) / ((SCREEN_HEIGHT / 1.4 + 20) - 64);
//        //假的导航栏的文字 逐渐变深
//        self.Navlabel.alpha = f * 2;
//        //image上的文字 逐渐变浅
//        self.label.alpha = 1 - (2 * f * 2);
//        //恢复到原本的imageview 的位置
//        self.imageview.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 1.4);
//        
//        [self.webview.scrollView addSubview:self.imageview];
//        [self.view bringSubviewToFront:self.Navlabel];
////        [self getbutton];
////        [self.back addSubview:self.imageview];
//        
//    }
//    [self.view bringSubviewToFront:self.back];
//    }
//}



-(void)backaaa
{
    [self.navigationController popViewControllerAnimated:YES];
//    NSLog(@"走走走 回家喽");
    
}
////判断用户交互方式的方法
//
//
//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//{
//    switch (navigationType)
//    {
//            //点击连接
//        case UIWebViewNavigationTypeLinkClicked:
//        {
//           // [self.imageview removeFromSuperview];
//            NSString *str = self.webview.request.URL.absoluteString;
//            [[UIApplication sharedApplication]openURL:[NSURL URLWithString:str]];
//            [self.imageview removeFromSuperview];
//            self.x += 1;
//            
//            
//            
//            
//            
//#pragma mark - 不启动本身的跳转
//  //       [self backaaa];
////            webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) {  TODO Auto-generated method stub view.loadUrl(url); return true; } });
//            NSLog(@"clicked");
//        }
//            break;
//            //提交表单
//        case UIWebViewNavigationTypeFormSubmitted:
//        {
//            NSLog(@"submitted");
//        }
//        default:
//            break;
//    }
//    return YES;
//}
-(void)pushController
{

    NSURLRequest *request1 =[NSURLRequest requestWithURL:[NSURL URLWithString:self.push]];
    [self.webview loadRequest:request1];
//    self.view
 //   NSString *str = [NSString stringWithFormat:@"%@=iphone",self.push];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:self.push]];
    [self.imageview removeFromSuperview];
//    UMSocialData *socialData = [[UMSocialData alloc] initWithIdentifier:@"identifier"];
//    UMSocialDataService *socialDataService = [[UMSocialDataService alloc] initWithUMSocialData:socialData];
//    BOOL isLike = socialData.isLike;
//    if ( isLike == YES) {
//        NSLog(@"已经收藏过了啊");
//    } else {
//    [socialDataService postAddLikeOrCancelWithCompletion:^(UMSocialResponseEntity *response){
//        //获取请求结果
//        NSLog(@"resposne is %@",response);
//    }];
//    }
//    //socialDataService为设置评论内容中初始化的对象
//    [socialDataService requestSocialDataWithCompletion:^(UMSocialResponseEntity *response){
//        // 下面的方法可以获取保存在本地的评论数，如果app重新安装之后，数据会被清空，可能获取值为0
//        int likeNumber = [socialDataService.socialData getNumber:UMSNumberLike];
//        NSLog(@"likeNum is %d",likeNumber);
//    }];
}



-(void)creatTabbar
{
    self.collectbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.collectbutton.frame = CGRectMake(0, SCREEN_HEIGHT - 49, SCREEN_WIDTH, 49);
    self.collectbutton.backgroundColor = NAVCOLOR;
    [self.collectbutton.layer setCornerRadius:5];
    self.collectbutton.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.collectbutton.titleLabel.textColor = [UIColor whiteColor];
    self.collectbutton.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.collectbutton setTitle:@"前往购买" forState:UIControlStateNormal];
    [self.collectbutton addSubview: self.collectbutton.titleLabel];
    [self.collectbutton addTarget:self action:@selector(pushController) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.collectbutton];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
